//
//  CZMainViewController.swift
//  Blog
//
//  Created by whitcast on 16/7/9.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SVProgressHUD

class CZMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //MARK: --为什么执行了代理才能显示自定义tabbar中的button加号按钮
        
        //创建自定义tabbar
        let czTabbar = CZTabBar()
        // 设置代理
       czTabbar.czDelegate = self
        
        //传入指定闭包
        czTabbar.composeButtonClosure = { [weak self] in
            if !CZUserAccountViewModel.shareUserAccount.islogin{
                SVProgressHUD.showErrorWithStatus("请先登录，亲~")
                return
            }
            if let weakSelf = self {
                let composeView = CZComposeView()
                composeView.show(weakSelf)
            }

           
        }
        
        setValue(czTabbar, forKey: "tabBar")
        
        
        addChildViewController(CZHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(CZMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(CZDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(CZProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
    }
    //重载addChildViewController
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        //  设置自定义tabBarItem
        childController.tabBarItem = CZTabBarItem()

        // 设置tabar的文字和显示图片
        
        //设置标题
        childController.title = title
        //设置图片
        childController.tabBarItem.image = UIImage(named: imageName)
        //设置渲染
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //设置tabbar字体颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        //设置字体
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14)], forState: .Normal)
        
        //添加子视图控制器
        let nav = UINavigationController(rootViewController: childController)
        
        addChildViewController(nav)
        
    }
    
}
extension CZMainViewController: CZTabBarDelegate {
    func didSelectedComposeButton() {
        print("我是代理对象调用过来的")
    }
    
}

